#include <iostream>
#include <string>
#include <memory>

#include "TcpServer.hpp"
#include "Socket.hpp"
#include "Commom.hpp"
#include "Accepter.hpp"
using namespace Net_Work;

void test(const ConnectionPtr &conn)
{
    std::cout << "test listen ..." << std::endl;
}

const int gblock = 32;
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage:\t" << argv[0] << " port" << std::endl;
        exit(-1);
    }
    uint16_t serverport = std::stoi(argv[1]);
    // 1.创建tcp连接
    std::unique_ptr<Socket> listensock = std::make_unique<TcpSocket>();
    listensock->BuildListenSocketMethod(serverport, gblock);
    SetNonBlock(listensock->GetSockfd());
    // 2.创建tcpserver
    std::unique_ptr<TcpServer> svr = std::make_unique<TcpServer>();
    // 3.创建连接器
    std::unique_ptr<Accept> accepter = std::make_unique<Accept>();
    auto recver = std::bind(&Accept::AcceptConnection, accepter.get(), std::placeholders::_1);
    // 4..添加事件
    ConnectionPtr listenconn = ConnectionFactory::BuildListenConn(listensock->GetSockfd(), EPOLLIN | EPOLLET, recver, svr.get());
    svr->AddConnection(listenconn);
    // 5..事件派发
    svr->Dispatcher();
    return 0;
}
